Add the serialiser: objects, a Writer, and graph copying - #5
Merged
Conversation
The exact inverse of the parser, and the thing every operation on an existing file needs. It lives beside the object model rather than in a module of its own, because splitting a model from its serialiser is how the two drift apart. AppendObject renders any object in PDF syntax with dictionary keys in order, so the same object always produces the same bytes and two files can be compared. Writer hands out object numbers, writes objects under them, and lays down a cross-reference table and a trailer. Copy walks an object graph out of a document into the file being built, renumbering as it goes and remembering what it has already copied, so shared objects stay shared, a /Parent pointing back at its own tree node terminates, and graphs from several documents can be merged without their numbers colliding. Verified by rewriting the corpus rather than by assertion: each of the 118 833 files that open is copied into a new file, re-read, and compared on page count, media boxes and the bytes of every content stream. All 118 833 match — 35.5 GB in, 35.2 GB out, twenty-eight seconds. That comparison found the one real defect: a producer that writes -0 for a coordinate. Negative zero is legal, meaningless, and rendered by strconv as "-0", which reads back as the integer zero — so a rewrite was not a fixpoint. Zero is now written as zero whatever its sign. It was forty-three files out of 118 833, and no amount of unit testing would have suggested looking. Exact 100% statement coverage, go vet clean, nine cross-compilation targets. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The exact inverse of the parser, and the thing every operation on an existing
file needs. It lives beside the object model rather than in a module of its
own, because splitting a model from its serialiser is how the two drift apart.
AppendObject renders any object in PDF syntax with dictionary keys in order, so
the same object always produces the same bytes and two files can be compared.
Writer hands out object numbers, writes objects under them, and lays down a
cross-reference table and a trailer. Copy walks an object graph out of a
document into the file being built, renumbering as it goes and remembering what
it has already copied, so shared objects stay shared, a /Parent pointing back
at its own tree node terminates, and graphs from several documents can be
merged without their numbers colliding.
Verified by rewriting the corpus rather than by assertion: each of the 118 833
files that open is copied into a new file, re-read, and compared on page count,
media boxes and the bytes of every content stream. All 118 833 match — 35.5 GB
in, 35.2 GB out, twenty-eight seconds.
That comparison found the one real defect: a producer that writes -0 for a
coordinate. Negative zero is legal, meaningless, and rendered by strconv as
"-0", which reads back as the integer zero — so a rewrite was not a fixpoint.
Zero is now written as zero whatever its sign. It was forty-three files out of
118 833, and no amount of unit testing would have suggested looking.
Exact 100% statement coverage, go vet clean, nine cross-compilation targets.